<?php

/**
 * Description of SubdomainModel
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
final class SubdomainModel extends Subdomain {

    /**
     * The constructor.
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
    }

    /**
     * ADMIN SPECIFIC METHODS.
     */

    /**
     * Getting data for all subdomains within a given domain.
     * Returns paginated results in descending order.
     *
     * @param int $domainId
     * @param int $pageNumber
     * @return array
     */
    public static function getSubdomainData($domainId, $searchString = NULL, $pageNumber = 1) {

        /**
         * Get paginated results.
         */
        $queryCount = new Query('
            SELECT COUNT(*) AS `count`
            FROM `subdomain`
            :where
            :and :search;
        ');
        $queryCount = AdminUtils::queryProcessing($queryCount, 'subdomain', 'domain', $domainId, $searchString);
        $query = new Query('
            SELECT :select
            FROM `subdomain`
            :join
            :where
            :and :search
            ORDER BY `subdomain`.`domain`, `subdomain`.`id`
            LIMIT :offset, :row_count;
        ');
        $query = AdminUtils::queryProcessing($query, 'subdomain', 'domain', $domainId, $searchString);

        $pagination = new Pagination(
            $query,
            $queryCount,
            $pageNumber
        );
        $output = array();
        $output['tabulated'] = $pagination->getRows();
        $output['navigation_array'] = $pagination->getNavigationArray();

        /**
         * Get domain data.
         */
        if ($domainId != NULL) {
            $domain = new Domain;
            $domain->load($domainId);
            $output['domain'] = $domain->getViewFields();
        }

        return $output;
    }

}